home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Acere (PowerPlant, Game) 1.2 / AcereÄ.sit / Acereƒ / Code / WellStack.cp < prev    next >
Text File  |  1995-03-04  |  9KB  |  412 lines

  1. // ===========================================================================
  2. //    WellStack.cp                        ⌐1993 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    Class for an object that can draw itself and respond to mouse clicks
  6.  
  7. #ifdef PowerPlant_PCH
  8. #include PowerPlant_PCH
  9. #endif
  10.  
  11. #include "WellStack.h"
  12. #include "CardDeck.h"
  13. #include "CardWell.h"
  14. #include "AcereApp.h"
  15. #include "CTextDoc.h"
  16. #include <LView.h>
  17. #include <LStream.h>
  18. #include <PP_Messages.h>
  19. #include <UDrawingState.h>
  20.  
  21.  
  22. extern CTextDoc *theDoc;
  23. extern CardDeck    *theDeck;
  24.  
  25. extern    unsigned long     ourAvailRam;
  26.  
  27. // ---------------------------------------------------------------------------
  28. //        Ñ CreatePaneStream [static]
  29. // ---------------------------------------------------------------------------
  30. //    Return a new Pane object initialized using data from a Stream
  31.  
  32. WellStack*
  33. WellStack::CreateWellStack(
  34.     LStream    *inStream)
  35. {
  36.     return (new WellStack(inStream));
  37. }
  38.  
  39.  
  40. // ---------------------------------------------------------------------------
  41. //        Ñ WellStack()
  42. // ---------------------------------------------------------------------------
  43. //    Default Constructor
  44.  
  45. WellStack::WellStack() : CardWell()
  46. {    
  47.     InitCard();
  48. }
  49.  
  50.  
  51. // ---------------------------------------------------------------------------
  52. //        Ñ WellStack(const WellStack&)
  53. // ---------------------------------------------------------------------------
  54. //    Copy Constructor
  55.  
  56. WellStack::WellStack(const WellStack    &inOriginal) : CardWell(inOriginal)
  57. {
  58.     InitCard();
  59. }
  60.  
  61.  
  62. // ---------------------------------------------------------------------------
  63. //        Ñ WellStack(SPaneInfo&)
  64. // ---------------------------------------------------------------------------
  65. //    Construct Pane from data in a struct
  66.  
  67. WellStack::WellStack(const SPaneInfo    &inPaneInfo) : CardWell(inPaneInfo)
  68. {
  69.     InitCard();
  70. }
  71.  
  72.  
  73. // ---------------------------------------------------------------------------
  74. //        Ñ WellStack(LStream*)
  75. // ---------------------------------------------------------------------------
  76. //    Construct Pane from data in a Stream
  77.  
  78. WellStack::WellStack(
  79.     LStream    *inStream) : CardWell(inStream)
  80. {
  81.     InitCard();
  82. }
  83.  
  84.  
  85. // ---------------------------------------------------------------------------
  86. //        Ñ InitPane
  87. // ---------------------------------------------------------------------------
  88. //    Initialize Pane from data in a struct
  89.  
  90. void
  91. WellStack::InitCard()
  92. {
  93.     itsCards = nil;
  94.     InitCards();
  95. }
  96.  
  97.  
  98. // ---------------------------------------------------------------------------
  99. //        Ñ ~WellStack
  100. // ---------------------------------------------------------------------------
  101. //    Destructor
  102.  
  103. WellStack::~WellStack()
  104. {
  105.     PutInside(nil);
  106.     
  107.     if (sLastPaneClicked == this)
  108.     {
  109.         sLastPaneClicked = nil;
  110.     }
  111.     
  112. /*    if (itsCards != nil)
  113.     {
  114.         PlayingCard    *currentCard;
  115.         short        i, k;
  116.         
  117.         k = itsCards->GetCount();
  118.         
  119.         for (i=1;i<=k; i++)
  120.         {
  121.             itsCards->FetchItemAt(i, ¤tCard);
  122.             delete currentCard;
  123.         }
  124.         
  125.         delete itsCards;
  126.     }
  127.     itsCards = nil;
  128.     
  129.     if (itsCard != nil)
  130.         delete itsCard;
  131. */
  132. }
  133.  
  134.  
  135. // ---------------------------------------------------------------------------
  136. //        Ñ Draw
  137. // ---------------------------------------------------------------------------
  138. //    Try to draw contents of a Pane
  139. //
  140. //    inSuperDrawRgnH specifies, in Port coordinates, the portion of the
  141. //    Pane's SuperView that needs to be drawn. Specify nil to bypass
  142. //    the intersection test.
  143. //    
  144. //    This is a wrapper function which calls DrawSelf if it is proper for
  145. //    the Pane to draw. This means that:
  146. //        > Pane's Visible property is on
  147. //        > Pane can be focused
  148. //        > Pane's Frame is in QuickDraw space
  149. //        > Pane's Frame intersects inSuperDrawRgnH
  150.  
  151. void    WellStack::Draw(RgnHandle    inSuperDrawRgnH)
  152. {
  153.     Rect        frame/*, useFrame */;
  154.     
  155.     if ( IsVisible()  &&
  156.          FocusDraw()  &&
  157.          CalcPortFrameRect(frame)  &&
  158.          ((inSuperDrawRgnH == nil) || RectInRgn(&frame, inSuperDrawRgnH)) )
  159.     {
  160.         DrawSelf();
  161.     }
  162. //    DisposHandle((Handle)currentCard);
  163. }
  164.  
  165. void    WellStack::DrawSelf()
  166. {
  167.     Rect        frame, useFrame;
  168.     
  169.     short        i, maxItems;
  170. /*    RgnHandle    saveClipRgn, ourRectRgn;
  171.     
  172.     CalcPortFrameRect(frame);
  173.  
  174.     saveClipRgn = NewRgn();            //    get an empty region
  175.     GetClip( saveClipRgn );            //    save current
  176.     
  177.     ourRectRgn = NewRgn();
  178.     RectRgn(ourRectRgn, &frame);
  179.     SetClip( ourRectRgn );            //    install desired clipRgn
  180.      */
  181.     CalcPortFrameRect(frame);
  182.         
  183. //    PlayingCard    **currentCard = (PlayingCard **) NewHandle(4L);
  184.     PlayingCard    *currentCard;
  185.         
  186.     if ((itsCard == nil) || (itsCards == nil))
  187.         maxItems = 0;
  188.     else if (itsCard->card == kNoCard)
  189.         maxItems = 0;
  190.     else
  191.         maxItems = (short) itsCards->GetCount();
  192.  
  193.     CalcPortFrameRect(frame);
  194.     
  195.     if (maxItems == 0)
  196.     {
  197.         useFrame = frame;
  198.         useFrame.bottom = useFrame.top + (theDeck->GetCardHeight());
  199.         EraseRect(&useFrame);
  200.         FrameRoundRect(&useFrame, 20, 20);
  201.         
  202.     }
  203.     for (i=1; i<= maxItems; i++)
  204.     {
  205.         itsCards->FetchItemAt(i, ¤tCard);
  206.         
  207.         if (i == maxItems)
  208.         {
  209.             useFrame = frame;
  210.             useFrame.top = useFrame.bottom - (theDeck->GetCardHeight());
  211.         }
  212.         else
  213.         {
  214.             useFrame = frame;
  215.             useFrame.top = frame.top + (kStubHeight  * (i-1));
  216.             useFrame.bottom = useFrame.top + kStubHeight;
  217.         }
  218.         
  219.         if (currentCard == nil)
  220.         {
  221.             EraseRect(&frame);
  222.             FrameRoundRect(&useFrame, 20, 20);
  223.         }
  224.         else if (currentCard->card == kNoCard)
  225.         {
  226.             EraseRect(&frame);
  227.             FrameRoundRect(&useFrame, 20, 20);
  228.         }
  229.         else
  230.         {
  231.             theDeck->DrawCard(currentCard, useFrame, isHighlighted);
  232. //                FrameRoundRect(&useFrame, 20, 20);
  233.         }
  234.     }
  235.  
  236.  
  237. //    SetClip( saveClipRgn );            /* restore previous value */
  238. //    DisposeRgn( saveClipRgn );        /* not needed any more */
  239. //    DisposeRgn( ourRectRgn );        /* not needed any more */
  240. }
  241.  
  242. void    WellStack::InitCards(void)
  243. {
  244.     //    done when initializing only
  245.     
  246.     short             i, k;
  247.     long            thePaneID = GetPaneID();
  248.     CardStruct        theTempCard;
  249. //    PlayingCard        **currentCard;
  250.     PlayingCard        *currentCard;
  251.     
  252. //    currentCard = (PlayingCard **) NewHandle(4L);
  253.     
  254.     theDoc->theStacks[theDoc->currentStack] = this;
  255.     
  256.     if (itsCards != nil)                //    we're restarting a game╔
  257.     {
  258.         k = itsCards->GetCount();
  259.         
  260.         for (i=1;i<=k; i++)
  261.         {
  262.             itsCards->FetchItemAt(i, ¤tCard);
  263.             delete currentCard;
  264.         }
  265.         delete itsCards;
  266.     }
  267.     
  268. //    itsCards = new LDynamicArray(4L);
  269.     itsCards = new LDynamicArray(4L);
  270.     mFrameSize.height -= kStubHeight;
  271.     
  272.     for (i=(thePaneID - 3001); i < NumCards; i += 8)
  273.     {
  274.         theTempCard.card = kNoCard;
  275.         currentCard = new PlayingCard(&theTempCard /*, this */);
  276.         
  277.         k = (short) theDeck->theCardPositions[i];
  278.         
  279.         theDeck->GetCardInfo(k, currentCard);
  280. //        tempCard->itsOwner = this;
  281. //        itsCard = tempCard;
  282.         
  283. //        itsCards->InsertItemsAt(1, 9999, &itsCard);
  284.         
  285.         AddCardToWell(nil, currentCard);
  286.         
  287. //        whichCard = k;
  288.     }
  289.     
  290.     theDoc->currentStack++;
  291.     
  292. //    DrawSelf();
  293. //    DisposHandle((Handle)currentCard);
  294.     
  295. //    Refresh();
  296. //    UpdatePort();
  297. }
  298.  
  299. Boolean    WellStack::CanDropOnSlot(PlayingCard *draggedCard)
  300. {
  301.     //    stack behavior is different: we need a card of an opposite color
  302.     //    and an immediately lower value.
  303.     
  304.     if ((draggedCard->color != itsCard->color) && 
  305.             (draggedCard->card == (itsCard->card -1)))
  306.         return (true);
  307.     else
  308.         return (false);
  309. }
  310.  
  311.  
  312. void    WellStack::AddCardToWell(CardWell *whichWell, PlayingCard *whichCard)
  313. {
  314.     whichCard->itsOwner = this; 
  315.     itsCards->InsertItemsAt(1, 9999, &whichCard);
  316.     itsCard = whichCard;
  317.     
  318.     AdjustFrame();
  319. }
  320.  
  321.  
  322. void    WellStack::RemoveCardFromWell(CardWell *whichWell, PlayingCard *whichCard)
  323. {
  324.     short            i, maxItems;
  325.     PlayingCard        *currentCard;
  326.     
  327. //    currentCard = (PlayingCard **) NewHandle(4L);
  328.     
  329. //    (*tempCard)->card = kNoCard;
  330.     
  331.     maxItems = (short) itsCards->GetCount();
  332.     
  333.     for (i = maxItems; i > 0; i--)
  334.     {
  335.         itsCards->FetchItemAt(i, ¤tCard);
  336.         
  337.         if (((currentCard)->suit == whichCard->suit) && 
  338.                     ((currentCard)->card == whichCard->card)) // right card
  339.         {
  340.             itsCards->RemoveItemsAt(1, i);
  341.             
  342.             if (i == maxItems)    //    need to update "top" card    
  343.             {
  344.                 maxItems = (short) itsCards->GetCount();
  345.                 
  346.                 if (maxItems == 0)
  347.                 {
  348.                     itsCard = nil;
  349.                 }
  350.                 else
  351.                     itsCards->FetchItemAt(maxItems, &itsCard);
  352.             }
  353.             AdjustFrame();
  354.             break;        //    exit for loop
  355.         }
  356.     }
  357.     
  358. //    DisposHandle((Handle)currentCard);
  359.     
  360. }
  361.  
  362. void    WellStack::AdjustFrame(void)
  363. {
  364.         short        maxItems, difference;
  365.         Rect        frame;
  366.         RgnHandle    newRgn, oldRgn, saveClipRgn;
  367.         
  368.     oldRgn = NewRgn();
  369.     OpenRgn();
  370.     CalcPortFrameRect(frame);
  371.     FrameRoundRect(&frame, 20, 20);
  372.     CloseRgn(oldRgn);
  373.  
  374.     newRgn = NewRgn();
  375.     
  376.     difference = mFrameSize.height;
  377.      
  378.     if (itsCards == nil)
  379.          maxItems = 0;
  380.     else
  381.         maxItems = (short) itsCards->GetCount();
  382.     mFrameSize.height = (theDeck->GetCardHeight()) - kStubHeight + (kStubHeight * maxItems);
  383.     
  384.     difference -= mFrameSize.height;
  385.     
  386.     CalcPortFrameRect(frame);
  387.     
  388.     OpenRgn();
  389.     FrameRoundRect(&frame, 20, 20);
  390.     CloseRgn(newRgn);
  391.     
  392.     CopyRgn(newRgn, itsCardRgn);
  393.     
  394.     if (difference > 0)
  395.     {
  396.         //    we need to clean up
  397.         
  398. //        DiffRgn(newRgn,oldRgn,newRgn);
  399.         DiffRgn(oldRgn, newRgn, newRgn);
  400.         
  401.         saveClipRgn = NewRgn();
  402.         
  403.         GetClip(saveClipRgn);
  404.         SetClip(newRgn);
  405.         EraseRgn(newRgn);
  406.         SetClip(saveClipRgn);
  407.         DisposeRgn(saveClipRgn);
  408.     }
  409.     DisposeRgn(newRgn);
  410.     DisposeRgn(oldRgn);
  411. }
  412.